#include <cstdio>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "projection.h"
#include "minIni.h"
#include "config.h"
#include "resources.h"
#include "map.h"
#include "spriteanimator.h"
#include "sprite.h"
#include "blitqueue.h"
#include "grcontext.h"
#include "entityaction.h"
#include "entity.h"
#include "entitymanager.h"
#include "inputhandler.h"
#include "controller.h"
#include "player.h"
#include "game.h"
#include "minIni.h"
#include "config.h"
#include "oribi.h"

using namespace Oribi;

MouseState::MouseState()
{
    for (int i = 0; i < N_MOUSE_BUTTONS; ++i)
    {
        buttons[i] = false;
    }
}

void InputHandler::handleInput(OribiApp& app, int *mLoopState)
{
    SDL_Event event;
    int buttonState;

    SDL_PumpEvents();
    while(SDL_PollEvent(&event))
    {
        buttonState = 0;
        switch (event.type)
        {
        case SDL_QUIT:
            *mLoopState = *mLoopState & ~RUNNING_BIT;
            break;
        case SDL_MOUSEBUTTONUP:
            buttonState = -1;
        case SDL_MOUSEBUTTONDOWN:
            buttonState++;

            switch (event.button.button)
            {
            case SDL_BUTTON_LEFT:
                mouse.buttons[0] = buttonState;
                break;
            case SDL_BUTTON_MIDDLE:
                mouse.buttons[1] = buttonState;
                break;
            case SDL_BUTTON_RIGHT:
                mouse.buttons[2] = buttonState;
                break;
            default:
                break;
            }
            break;
        }
    }

    SDL_GetMouseState(&mouse.position.x, &mouse.position.y);
    app.getGraphicContext().updateCamera(mouse);
}
